package gui;

import javax.swing.*;

import LoadAndSaveImage.ImagesLoading;
import soundCapture.Capture;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Create extends JFrame {
	JPanel panel = new JPanel();
	JButton bcreate = new JButton("Create");
	JButton biamge = new JButton("+Image");
	JButton baudio_capture = new JButton("+Audio");
	JButton baudio_stop = new JButton("Stop");
	JButton baudio_play = new JButton("Play");
	JTextField txtitle = new JTextField(15);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
	JFormattedTextField txdate = new JFormattedTextField(sdf);
	JTextField txgroup = new JTextField(15);
	JTextArea txdetails = new JTextArea();
	JScrollPane scroll = new JScrollPane(txdetails,
			JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
			JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	JLabel title = new JLabel("Title:");
	JLabel date = new JLabel("Date:");
	JLabel group = new JLabel("Group:");
	JLabel details = new JLabel("Deatils:");
	JLabel audio_mssg = new JLabel("");
	JFrame fr = new JFrame();
	Capture audio = new Capture();
	FileDialog fd = new FileDialog(fr, "Open", FileDialog.LOAD);
	ImagesLoading a = new ImagesLoading(300, 400);
	Image Image1;
	Canvas c = new Canvas() {
		public void paint(Graphics g) {
			if (Image1 != null)
				g.drawImage(Image1, 0, 0, this);
		}
	};

	public Create() {
		createFrame();
	}

	public void createFrame() {
		setSize(600, 400);
		setLocation(500, 280);
		setResizable(false);
		c.setSize(300, 400);
		c.setBounds(300, 0, 300, 400);
		panel.setLayout(null);

		/* Here you can change the location & size of the Lables */
		title.setBounds(20, 20, 150, 20);
		date.setBounds(20, 70, 150, 20);
		group.setBounds(20, 120, 150, 20);
		details.setBounds(20, 170, 150, 20);
		audio_mssg.setBounds(20, 330, 150, 20);

		/* Here you can change the location & size of the textField */
		txtitle.setBounds(100, 20, 200, 20);
		txdate.setBounds(100, 70, 200, 20);
		txgroup.setBounds(100, 120, 200, 20);
		scroll.setBounds(100, 170, 200, 110);
		txdetails.setLineWrap(true);
		txdetails.setWrapStyleWord(true);

		/* Here you can change the location & size of the buttons */
		bcreate.setBounds(220, 300, 80, 20);
		biamge.setBounds(100, 300, 80, 20);
		baudio_capture.setBounds(100, 330, 80, 20);
		baudio_stop.setBounds(220, 330, 80, 20);
		baudio_stop.setVisible(false);
		baudio_play.setBounds(220, 330, 80, 20);
		baudio_play.setVisible(false);

		txdate.setUI(new HintTextFieldUI("YYYY/MM/DD", true));

		panel.add(title);
		panel.add(date);
		panel.add(group);
		panel.add(details);
		panel.add(audio_mssg);
		panel.add(txtitle);
		panel.add(txdate);
		panel.add(txgroup);
		panel.add(scroll);
		panel.add(bcreate);
		panel.add(biamge);
		panel.add(baudio_capture);
		panel.add(baudio_stop);
		panel.add(baudio_play);
		panel.add(c);
		JPanel BgPanel = new BgPanel();
		BgPanel.setLayout(new BorderLayout());
		panel.setOpaque(false);
		BgPanel.add(panel, BorderLayout.CENTER);
		getContentPane().add(BgPanel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

		bcreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
				Date date = new Date();
				String filename = Login.user + "-" + dateFormat.format(date);
				createTask(filename);
				if (a.ImageSave(fd, filename + ".jpg")
						&& audio.saveAudio(filename))
					JOptionPane.showMessageDialog(fr, "Event Saved!", "Done",
							JOptionPane.INFORMATION_MESSAGE);
				JFrame menu = new Calendar1();
				menu.setVisible(true);
				dispose();
			}
		});

		biamge.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				a.ImageSelect(fd);
				Image1 = a.getImage();
				c.repaint();
			}
		});

		baudio_capture.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if (audio.isPlaying())
					audio.stopAudio();
				audio.captureAudio();
				audio_mssg.setText("Recording");
				baudio_capture.setEnabled(false);
				baudio_stop.setVisible(true);
				baudio_stop.setEnabled(true);
				baudio_play.setVisible(false);
				baudio_play.setEnabled(false);
				repaint();
			}
		});
		baudio_stop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				audio.stopAudio();
				audio_mssg.setText("");
				baudio_capture.setEnabled(true);
				if (audio.canPlay()) {
					baudio_play.setVisible(true);
					baudio_play.setEnabled(true);
				}
				baudio_stop.setVisible(false);
				baudio_stop.setEnabled(false);
				repaint();
			}
		});
		baudio_play.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if (!audio.isPlaying()) {
					audio.playAudio();
					audio_mssg.setText("");
				}
			}
		});
	}

	public void createTask(String filename) {
		File create = new File(Login.data_dir + filename + ".txt");
		try {
			create.createNewFile();
		} catch (IOException ioe) {
			System.out.print(ioe);
		}

		try {
			java.io.PrintWriter lpw = new PrintWriter(new FileWriter(create,
					false));
			lpw.write(txtitle.getText() + "\r\n");
			lpw.write(txdate.getText() + "\r\n");
			lpw.write(txgroup.getText() + "\r\n");
			lpw.write(txdetails.getText() + "\r\n");
			lpw.close();
		} catch (IOException ioe) {
			System.out.print(ioe);
		}
	}

}
